<template>
	<view class="container">
		<view class="header">
			<view class="title">
				欢迎登录
			</view>
			<view class="info">
				请输入账号密码登录
			</view>
		</view>
		<view class="login">
			<view class="item">
				<view class="title flexCenter">
					<image
						src="@/static/images/login/account.png"
						mode="widthFix"
					></image>
					账号
				</view>
				<input
					type="text"
					placeholder="请输入登录账号"
					v-model.trim="form.username"
				>
			</view>
			<view class="item">
				<view class="title flexCenter">
					<image
						src="@/static/images/login/password.png"
						mode="widthFix"
					></image>
					密码
				</view>
				<input
					type="text"
					:password="true"
					placeholder="请输入登录密码"
					v-model.trim="form.password"
				>
			</view>
		</view>
		<button
			class="submit"
			@tap="login"
		>登录</button>
		<view class="other flexCenterCol">
			<view class="title">
				—— 其他方式登录 ——
			</view>

			<button class="wechat-login-btn" open-type="getPhoneNumber" @getphonenumber="getphonenumber">
				<view
					class="wechat flexCenterCol"
				>
					<image
						src="@/static/images/login/wechat.png"
						mode=""
					></image>
					<view class="info">
						微信登录
					</view>
				</view>
			</button>
		</view>
		<button
			class="other-login"
			@tap="otherLogin"
		>技术顾问账户</button>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
import { goPage, toast, setLocal, getLocal,removeLocal } from '@/utils/uni'
export default {
	data () {
		return {
			form: {
				username: '',
				password: '',
				loading: false
			}
		};
	},
	mounted() {
		removeLocal('token')
		removeLocal('user')
		removeLocal('userType')
		removeLocal('currentAddress')
		removeLocal('currentReceiveAddress')
		removeLocal('isAgree')
	},
	methods: {
		otherLogin(){
			uni.reLaunch({
				url: "/cmhqPage/webviewh/webviewh"
			})
		},
		getphonenumber (e) {
			console.log(e);
			let _this = this
			uni.showToast({
				title: '登录中...',
				loading: true,
				icon:'loading',
				duration: 2000
			});
			if(e.detail.errMsg === "getPhoneNumber:ok"){
				this.$http.get('wechat/loginByMobile', {
					code: e.detail.code
				}, function(res) {
					if (res.code === 'success') {
						if(res.data.errcode===0){
							uni.login({
								provider: 'weixin',
								success: (result) => {
									_this.$http.get('wechat/loginByJsCode', {
										jsCode: result.code,
										mobile:res.data.mobile
									}, function(res) {
										if (res.code === 'success') {
											_this.setlocalStorage(res.data)
										}else{
											_this.$uti.alert(res.message);
										}
									})
									uni.hideToast()
								},
								fail: (error) => {
									uni.hideToast()
								}
							})
						}else{
							_this.setlocalStorage(res.data)
						}
					}else{
						uni.hideToast()
						_this.$uti.alert(res.message);
					}
				})
			}
		},
		// 存储本地token,用户信息
		setlocalStorage(result){
			let expireTime = new Date().getTime() + 3 * 1000 * 60 * 60; // 设置一小时后过期
			setLocal('token',{token: result.token,expireTime})
			setLocal('userType',result.userType)
			if(result.userType === 'wechat'){
				setLocal('user',result)
			}else if(result.userType === 'system'){
				setLocal('user',result.user)
			}
			uni.reLaunch({
				url: "/pages/index/index"
			})
		},
		getRandomInt(minLength, maxLength){
      // 计算最小值和最大值
      const min = Math.pow(10, minLength - 1);
      const max = Math.pow(10, maxLength) - 1;
      // 生成随机数
      return Math.floor(Math.random() * (max - min + 1)) + min;
		},
		login () {
			console.log('---');
			let _this = this
			if(!this.form.username){
				this.$uti.alert('请输入账号')
				return
			}
			if(!this.form.password){
				this.$uti.alert('请输入密码')
				return
			}
			let random = this.getRandomInt(1,16)
			uni.showToast({
				title: '登录中...',
				loading: true,
				icon:'loading',
				duration: 2000
			});
			this.$http.post('app/auth/login', {
				username: this.form.username,
				password: this.form.password,
				code: "2", //固定传2
				uuid: `captcha-code:${random}`, //按照captcha-code:xxx的格式传值，xxx可以替换为任意字符串
				platform: "company" //固定company
			}, function(res) {
				console.log(res);
				
				if (res.token) {
					_this.setlocalStorage(res)
				} else {
					_this.$uti.alert(res.message);
				}
				_this.loading = false
				
			})

		}
	}
}
</script>

<style lang="scss" scoped>
/* 头部样式 */
.header {
	margin: 46rpx 0 94rpx 41rpx;

	.title {
		font-size: 60rpx;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #272729;
		text-align: left;
	}

	.info {
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		// color: #666666;
	}
}

/* 登录样式 */
.login {
	margin-left: 41rpx;
	margin-right: 53rpx;
	.item {
		border-bottom: 1px solid #e1e1e6;
		margin-bottom: 47rpx;
		padding-bottom: 17rpx;

		.title {
			font-size: 30rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #272729;
			margin-bottom: 34rpx;

			image {
				width: 36rpx;
				height: 36rpx;
				margin-right: 12rpx;
			}
		}

		input {
			width: 100%;
			height: 42rpx;
			font-size: 30rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			// color: #AEAEBD;
		}
	}
}
.submit {
	display: flex;
	justify-content: center;
	align-items: center;
	margin-bottom: 48rpx;
	width: 656rpx;
	height: 88rpx;
	background: linear-gradient(270deg, #ff0000 0%, #fe5454 100%);
	border-radius: 70rpx 70rpx 70rpx 70rpx;
	font-size: 32rpx;
	font-family: PingFang SC-Medium, PingFang SC;
	font-weight: 500;
	color: #ffffff;
}
.other-login{
	display: flex;
	justify-content: center;
	align-items: center;
	margin-bottom: 48rpx;
	width: 656rpx;
	height: 88rpx;
	border: 2rpx solid #ff0000;
	border-radius: 70rpx 70rpx 70rpx 70rpx;
	font-size: 32rpx;
}
.other {
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	margin-bottom: 48rpx;
	.title {
		font-size: 24rpx;
		font-family: PingFang-SC-Medium-, PingFang-SC-Medium;
		font-weight: normal;
		color: #aeaebd;
		margin-bottom: 29rpx;
	}
	.wechat-login-btn{
		background-color: transparent;
		border: none;
		line-height: normal;
		.wechat {
			image {
				width: 76rpx;
				height: 76rpx;
				margin-bottom: 13rpx;
			}
			.info {
				font-size: 24rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #272729;
			}
		}
	}
	.wechat-login-btn::after{
		border: none;
	}
}
</style>